package cn.lili.modules.goods.mapper;

import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 规格项数据处理层
 *
 * @author pikachu
 * @since 2020-02-18 15:18:56
 */
public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * 根据店铺ID获取商品ID列表
     *
     * @param storeId 店铺ID
     * @return 商品ID列表
     */
    @Select("SELECT id FROM li_goods WHERE store_id = #{storeId}")
    List<String> getGoodsIdByStoreId(String storeId);

    /**
     * 添加商品评价数量
     *
     * @param commentNum 评价数量
     * @param goodsId    商品ID
     */
    @Update("UPDATE li_goods SET comment_num = comment_num + #{commentNum} WHERE id = #{goodsId}")
    void addGoodsCommentNum(Integer commentNum, String goodsId);

    /**
     * 查询商品VO分页
     *
     * @param page         分页
     * @param queryWrapper 查询条件
     * @return 商品VO分页
     */
    @Select("select g.* from li_goods as g ")
    IPage<GoodsVO> queryByParams(IPage<GoodsVO> page, @Param(Constants.WRAPPER) Wrapper<GoodsVO> queryWrapper);

    @Select("<script>" +
            "SELECT " +
                "lg.*, " +
                "MIN(lgs.id) AS skuId " +
            "FROM li_goods lg " +
            "LEFT JOIN li_goods_sku lgs ON lg.id = lgs.goods_id " +
            "<where>" +
                "lg.auth_flag = 'PASS' AND lg.market_enable = 'UPPER' AND lg.delete_flag='false'"+
                "<if test='goodsSearchParams.keyword != null and goodsSearchParams.keyword != \"\"'>" +
                    "AND lg.goods_name LIKE CONCAT('%', #{goodsSearchParams.keyword}, '%') " +
                "</if>" +
                "<if test='goodsSearchParams.storeName != null and goodsSearchParams.storeName != \"\"'>" +
                    "AND lg.store_name LIKE CONCAT('%', #{goodsSearchParams.storeName}, '%') " +
                "</if>" +
                "<if test='goodsSearchParams.categoryId != null and goodsSearchParams.categoryId != \"\"'>" +
                    "AND lg.category_path LIKE CONCAT('%', #{goodsSearchParams.categoryId}, '%') " +
                "</if>" +
                "<if test='goodsSearchParams.recommend != null'>" +
                    "AND lg.recommend = ${goodsSearchParams.recommend} " +
                "</if>" +
                "<if test='goodsSearchParams.storeId != null'>" +
                    "AND lg.store_id = #{goodsSearchParams.storeId} " +
                "</if>" +
                "<if test='goodsSearchParams.salesModel != null'>" +
                    "AND lg.sales_model = #{goodsSearchParams.salesModel} " +
                "</if>" +
                "<if test='goodsSearchParams.leQuantity != null'>" +
                    "AND lg.quantity le #{goodsSearchParams.leQuantity} " +
                "</if>" +
                "<if test='goodsSearchParams.geQuantity != null'>" +
                    "AND lg.quantity ge #{goodsSearchParams.geQuantity} " +
                "</if>" +
                "<if test='goodsSearchParams.goodsType != null'>" +
                    "AND lg.goods_type = #{goodsSearchParams.goodsType} " +
                "</if>" +
            "</where>" +
            "GROUP BY lg.id" +
                "<if test='goodsSearchParams.sort != null'>" +
                    "<choose>" +
                        "<when test='goodsSearchParams.sort == \"commentNum\"'>" +
                            " ORDER BY lg.comment_num DESC " +
                        "</when>" +
                        "<when test='goodsSearchParams.sort == \"releaseTime\"'>" +
                            " ORDER BY lg.release_time DESC " +
                        "</when>" +
                        "<when test='goodsSearchParams.sort == \"buyCount\"'>" +
                            " ORDER BY lg.buy_count DESC " +
                        "</when>" +
                        "<when test='goodsSearchParams.sort == \"price\"'>" +
                            " ORDER BY lg.cost ${goodsSearchParams.order} " +
                        "</when>" +
                    "</choose>" +
                "</if>" +
            "</script>")
    IPage<Goods> getGoodsByPage(IPage<Goods> page, @Param("goodsSearchParams") GoodsSearchParams goodsSearchParams);

}